import { Channel, History } from '@/types/data'
import { Token } from '@/types/store'

const TOKEN_KEY = 'geek_h5_token'
const CHANNELS_KEY = 'geek_h5_chnnels_token'
const HISTORY_KEY = 'geek_h5_history_key'

/**
 * 获取token
 * @returns
 */
export const getToken = (): Token => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || '{}')
}

/**
 * 设置token
 * @param obj
 */
export const setToken = (obj: Token) => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(obj))
}

/**
 * 移除token
 */
export const removeToken = (): void => {
  localStorage.removeItem(TOKEN_KEY)
}

/**
 * 判断有无token
 * @returns
 */
export const hasToken = (): Boolean => {
  return !!getToken().token
}

//--------------------------------
//从本地获取 channels数据
export const getLocalChannels = (): Channel[] => {
  return JSON.parse(localStorage.getItem(CHANNELS_KEY) || '[]')
}

export const setLocalChannels = (channels: Channel[]) => {
  localStorage.setItem(CHANNELS_KEY, JSON.stringify(channels))
}
//----------------------------------
//获取本地历史
export const getLocalHistory = (): History => {
  return JSON.parse(localStorage.getItem(HISTORY_KEY) || '[]')
}

//设置本地历史
export const setLocalHistory = (arr: History) => {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(arr))
}

//移除本地历史
export const removeLocalHistory = (): void => {
  localStorage.removeItem(HISTORY_KEY)
}
